<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/28 0028
 * Time: 20:21
 */

namespace app\storage\controller;

use app\storage\model\SmtInflowModel;
use app\storage\model\SmtOutflowModel;
use app\storage\service\CommonService;
use app\storage\service\FeedService;
use cmf\controller\AdminBaseController;
use think\Request;
use app\storage\model\FeedModel;
use app\storage\model\FeedDetailModel;
class FeedController extends AdminBaseController
{

    private $feedService;
    private $feedModel;
    private $commonService;
    private $feedDetailModel;
    private $smtInflowModel;
    private $smtOutflowModel;
    use \app\storage\util\Common;
    public function __construct(Request $request = null, FeedService $feedService, FeedModel $feedModel, FeedDetailModel $feedDetailModel,CommonService $commonService)
    {
        parent::__construct($request);
        $this->feedService = $feedService;
        $this->feedModel = $feedModel;
        $this->feedDetailModel = $feedDetailModel;
        $this->commonService = $commonService;
        $this->smtInflowModel = new SmtInflowModel();
        $this->smtOutflowModel = new SmtOutflowModel();
    }

    /***
     * 料站表视图
     */
    public function index()
    {
        $storageUuid = input('storageUuid');
        $storageInfo = $this->commonService->get_assign_storage($storageUuid);
        $assign['storageUuid'] = $storageUuid;
        $assign['storageInfo'] = $storageInfo;
        $this->assign($assign);
        return $this->fetch('index');
    }

    /***
     * @return mixed
     * 添加料站表视图
     */
    public function add()
    {
        return $this->fetch('add');
    }

    /****
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 贴片还料流水
     */
    public function inflow()
    {
        $storageUuid = input('storageUuid');
        $storageInfo = $this->commonService->get_assign_storage($storageUuid);
        $assign['storageUuid'] = $storageUuid;
        $assign['storageInfo'] = $storageInfo;
        $this->assign($assign);
        return $this->fetch('inflow');
    }

    /****
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 贴片出库流水
     */
    public function outflow(){
        $storageUuid = input('storageUuid');
        $storageInfo = $this->commonService->get_assign_storage($storageUuid);
        $assign['storageUuid'] = $storageUuid;
        $assign['storageInfo'] = $storageInfo;
        $this->assign($assign);
        return $this->fetch('outflow');
    }


    /***
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取料站表信息（展示）
     */
    public function getFeedStation()
    {
        $where = $this->getWhere();
        $result = $this->feedModel->getFeed($where);
        return json($result);
    }

    /***
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取料站细表信息（展示）
     */
    public function getFeedStationDetail(){
        $condition = $this->getWhere();
        $feedUuid = input('feedUuid');
        $feed = $this->feedModel->findFeed(['uuid'=>$feedUuid])->toArray();//根据传入的料站表的uuid查找料站表的工单号（feed_id)
        $feedId = $feed['feed_id'];

        $condition['where'] =['feed_id'=>$feedId];
        $result = $this->feedDetailModel->getFeedDetail($condition);
        return json($result);
    }

    /***
     * @return \think\response\Json
     * 获取贴片出库数据
     */
    public function getFeedOutflow(){
        $where = $this->getWhere();
        $result = $this->smtOutflowModel->getSmtOutflow($where);
        return json($result);
    }

    /***
     * @return \think\response\Json
     * 获取贴片出库数据
     */
    public function getFeedInflow(){
        $where = $this->getWhere();
        $result = $this->smtInflowModel->getSmtInflow($where);
        return json($result);
    }
    /***
     * @return \think\response\Json
     * 通过excel 增加料站表信息
     */
    public function addByExcel()
    {
        $result = $this->feedService->addByExcel();
        return json($result);
    }
}